//
// Created by F1997 on 2025/4/9.
//

#include "dce_decode.h"


void dce_decode::parse_packet_dce(const struct pcap_pkthdr* pkthdr, const u_char* packet, MarketData& marketData) {
    std::cout << "解析数据包" << std::endl;
    // 输出原始数据包内容，以十六进制形式输出
    for (int i = 0; i < pkthdr->caplen; i++) {
        printf("%02x ", packet[i]);
        if ((i + 1) % 16 == 0) {
            printf("\n");
        }
    }
    printf("\n=================================\n");

    // 解析以太网头部
    struct my_ether_header *eth_hdr = (struct my_ether_header *)packet;
    if (ntohs(eth_hdr->ether_type) == /*0*/0x0800) {
        // 解析IP头部
        struct my_ip_header *ip_hdr = (struct my_ip_header *)(packet + sizeof(struct my_ether_header));
        //printf("ip_v: %d\n", ip_hdr->ip_v);
        //printf("ip_src: %s\n", inet_ntoa(ip_hdr->ip_src));
        //printf("ip_dst: %s\n", inet_ntoa(ip_hdr->ip_dst));
        if (ip_hdr->ip_p == IPPROTO_TCP) {
            // 解析TCP头部
            struct my_tcp_header *tcp_hdr = (struct my_tcp_header *)(packet + sizeof(struct my_ether_header) + (ip_hdr->ip_hl * 4));

            // 计算payload的起始位置
            int payload_offset = sizeof(struct my_ether_header) + (ip_hdr->ip_hl * 4) + (tcp_hdr->th_off * 4);
            int payload_size = pkthdr->len - payload_offset;

/*                // 打印基本信息
                printf("Source IP: %s\n", inet_ntoa(ip_hdr->ip_src));
                printf("Destination IP: %s\n", inet_ntoa(ip_hdr->ip_dst));
                printf("Source Port: %d\n", ntohs(tcp_hdr->th_sport));
                printf("Destination Port: %d\n", ntohs(tcp_hdr->th_dport));
                printf("Payload Size: %d bytes\n", payload_size);

                // 打印payload
                if (payload_size > 0) {
                    printf("Payload:\n");
                    for (int i = 0; i < payload_size; ++i) {
                        printf("%02x ", packet[payload_offset + i]);
                        if ((i + 1) % 16 == 0) {
                            printf("\n");
                        }
                    }
                    printf("\n");
                }*/

        }
        else if (ip_hdr->ip_p == IPPROTO_UDP) { //
            struct my_udp_header *udp_hdr = (struct my_udp_header *)(packet + sizeof(struct my_ether_header) + (ip_hdr->ip_hl * 4));
            // 计算payload的起始位置
            int payload_offset = sizeof(struct my_ether_header) + (ip_hdr->ip_hl * 4) + sizeof(struct my_udp_header); // udp包头长度固定为8？？？
            int payload_size = pkthdr->len - payload_offset; // 本个报文
            if (payload_size > 25) {
                // dmdp报文头，每个行情都以合约号域始
                struct DMDPHeader *dmdpHeader = (struct DMDPHeader *)(packet + payload_offset);

                // 打印原始payload数据
                for (int i = 0; i < 32; ++i) {
                    printf("%02x ", packet[payload_offset + i]);
                    if ((i + 1) % 16 == 0) {
                        printf("\n");
                    }
                }
                printf("\n------\n");

                printf("ip_src: %s\n", inet_ntoa(ip_hdr->ip_src)); // ip
                printf("ip_dst: %s\n", inet_ntoa(ip_hdr->ip_dst));
                printf("DstPort: %d\n", udp_hdr->DstPort); // 端口
                printf("SrcPort: %d\n", udp_hdr->SrcPort);
                printf("pkthdr.len: %d\n", pkthdr->len); // 长度
                printf("udp_hdr.len: %d\n", udp_hdr->len); // 长度
                printf("checkSum: %d\n", udp_hdr->checkSum);

                printf("payload_offset: %d\n", payload_offset);
                printf("payload_size: %d\n", payload_size);

                printf("---\n");
                printf("pkg_size: %d\n", dmdpHeader->pkg_size);
                std::cout << "pkg_type: " << ntohs(dmdpHeader->pkg_type) << std::endl; // 0x0002
                //printf("pkg_type: %d\n", dmdpHeader->pkg_type); // l2行情=pkg_type=0x0003
                printf("version: %d\n", dmdpHeader->version);
                printf("flag: %d\n", dmdpHeader->flag);
                printf("mdg_no: %d\n", dmdpHeader->mdg_no);
                printf("start_seq_no: %d\n", dmdpHeader->start_seq_no);
                printf("start_seq_no: %llu\n", ntohs(dmdpHeader->start_seq_no));
                printf("seq_num: %d\n", dmdpHeader->seq_num);
                printf("send_time: %llu\n", dmdpHeader->send_time);
                printf("send_time: %llu\n", ntohs(dmdpHeader->send_time));
                printf("reserved: %d\n", dmdpHeader->reserved);
                printf("=================================\n");

                // 包内的数据提取到 MarketData
                //marketData.snap_time = mirpHeader->snap_time;
                //marketData.snap_millisec = mirpHeader->snap_millisec;
                //marketData.packet_no = mirpHeader->packet_no;

                // 进一步解析域串
            }
        }
    }
}
